import { Service } from 'egg';

export default class roleService extends Service {

    public async getList() {
        const {ctx} = this;
        const roleList = await ctx.model.Role.find({})
        return roleList
    }
    public async save(data){
        const {ctx} = this;
        let result:boolean=false;
        await ctx.model.Role.create(data)
            .then(()=>{
                result = true;
            })
            .catch(()=>{
                result = false;
            })
        return result;
    }
    async update(roleData:object) {
        const {ctx} = this;
        let query={_id: roleData['_id']}
        let result;
        result = ctx.helper.updateByQuery(query, roleData,ctx.model.Role).then(res=>{
            return res
        }).catch(err=>{
            console.log(err);
        });
        return result;
    }
    public async delete(id:string) {
        const {ctx} = this;
        let whereStr = {'_id': id};
        let result:object={};

        await ctx.model.Role.remove(whereStr).then((res:object)=>{
            result = res;
        }).catch((err:any)=>{
            console.log(err)
        })
        return result
    }
}
